package constant.milk.toilet.setting;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import constant.milk.toilet.R;
import constant.milk.toilet.dialog.MilkDialog;
import constant.milk.toilet.util.MilkUtil;

public class SettingPasswordActivity extends Activity implements OnClickListener
{
	public static final String PASSWORD_DATA = "PASSWORD_DATA";
	
	private TextView messageTextView;
	
	private TextView icon1TextView;
	private TextView icon2TextView;
	private TextView icon3TextView;
	private TextView icon4TextView;
	
	private View text1View;
	private View text2View;
	private View text3View;
	private View text4View;
	private View text5View;
	private View text6View;
	private View text7View;
	private View text8View;
	private View text9View;
	private View textCencelView;
	private View text0View;
	private View textBackView;
	
	private String password;
	
	private String tempPassword;
	
	private MilkDialog dialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.password_activity);
		
		setFindViewId();
		setOnClick();
		setData();
	}
	
	private void setFindViewId()
	{
		messageTextView = (TextView) findViewById(R.id.passwordMessageTextView);
		
		icon1TextView = (TextView) findViewById(R.id.passwordIcon1TextView);
		icon2TextView = (TextView) findViewById(R.id.passwordIcon2TextView);
		icon3TextView = (TextView) findViewById(R.id.passwordIcon3TextView);
		icon4TextView = (TextView) findViewById(R.id.passwordIcon4TextView);
		
		text1View = findViewById(R.id.passwordText1View);
		text2View = findViewById(R.id.passwordText2View);
		text3View = findViewById(R.id.passwordText3View);
		text4View = findViewById(R.id.passwordText4View);
		text5View = findViewById(R.id.passwordText5View);
		text6View = findViewById(R.id.passwordText6View);
		text7View = findViewById(R.id.passwordText7View);
		text8View = findViewById(R.id.passwordText8View);
		text9View = findViewById(R.id.passwordText9View);
		textCencelView = findViewById(R.id.passwordTextCencelView);
		text0View = findViewById(R.id.passwordText0View);
		textBackView = findViewById(R.id.passwordTextBackView);
	}
	
	private void setOnClick()
	{
		text1View.setOnClickListener(this);
		text2View.setOnClickListener(this);
		text3View.setOnClickListener(this);
		text4View.setOnClickListener(this);
		text5View.setOnClickListener(this);
		text6View.setOnClickListener(this);
		text7View.setOnClickListener(this);
		text8View.setOnClickListener(this);
		text9View.setOnClickListener(this);
		textCencelView.setOnClickListener(this);
		text0View.setOnClickListener(this);
		textBackView.setOnClickListener(this);
	}
	
	private void setData()
	{
		setIcon1Def();
		setIcon2Def();
		setIcon3Def();
		setIcon4Def();
		
		password = "";
		tempPassword = "";
		
		messageTextView.setBackgroundResource(R.drawable.text_password);
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.passwordText1View:
				if (password.length() > 3)
				{
					setCheckPassword();
					return;
				}
				password += "1";
				setPush();
				break;
			case R.id.passwordText2View:
				if (password.length() > 3)
				{
					setCheckPassword();
					return;
				}
				password += "2";	
				setPush();
				break;
			case R.id.passwordText3View:
				if (password.length() > 3)
				{
					setCheckPassword();
					return;
				}
				password += "3";
				setPush();
				break;
			case R.id.passwordText4View:
				if (password.length() > 3)
				{
					setCheckPassword();
					return;
				}
				password += "4";
				setPush();
				break;
			case R.id.passwordText5View:
				if (password.length() > 3)
				{
					setCheckPassword();
					return;
				}
				password += "5";
				setPush();
				break;
			case R.id.passwordText6View:
				if (password.length() > 3)
				{
					setCheckPassword();
					return;
				}
				password += "6";
				setPush();
				break;
			case R.id.passwordText7View:
				if (password.length() > 3)
				{
					setCheckPassword();
					return;
				}
				password += "7";
				setPush();
				break;
			case R.id.passwordText8View:
				if (password.length() > 3)
				{
					setCheckPassword();
					return;
				}
				password += "8";
				setPush();
				break;
			case R.id.passwordText9View:
				if (password.length() > 3)
				{
					setCheckPassword();
					return;
				}
				password += "9";
				setPush();
				break;
			case R.id.passwordTextCencelView:
				password = "";
				setIcon1Def();
				setIcon2Def();
				setIcon3Def();
				setIcon4Def();
				break;
			case R.id.passwordText0View:
				if (password.length() > 3)
				{
					setCheckPassword();
					return;
				}
				password += "0";
				setPush();
				break;
			case R.id.passwordTextBackView:
				if (password.equals(""))
				{
					return;
				}
				setBank();
				break;
		}
	}
	
	private void setPush()
	{
		if (password.length() < 2)
		{
			setIcon1Push();
			setIcon2Def();
			setIcon3Def();
			setIcon4Def();
		}
		else if (password.length() < 3)
		{
			setIcon1Push();
			setIcon2Push();
			setIcon3Def();
			setIcon4Def();
		}
		else if (password.length() < 4)
		{
			setIcon1Push();
			setIcon2Push();
			setIcon3Push();
			setIcon4Def();
		}
		else if (password.length() < 5)
		{
			setIcon1Push();
			setIcon2Push();
			setIcon3Push();
			setIcon4Push();
			
			setCheckPassword();
		}
	}
	
	private void setBank()
	{
		if (password.length() < 2)
		{
			setIcon1Def();
			setIcon2Def();
			setIcon3Def();
			setIcon4Def();
			
			password = "";
		}
		else if (password.length() < 3)
		{
			setIcon1Push();
			setIcon2Def();
			setIcon3Def();
			setIcon4Def();
			
			password = password.substring(0, 1);
		}
		else if (password.length() < 4)
		{
			setIcon1Push();
			setIcon2Push();
			setIcon3Def();
			setIcon4Def();
			
			password = password.substring(0, 2);
		}
		else if (password.length() < 5)
		{
			setIcon1Push();
			setIcon2Push();
			setIcon3Push();
			setIcon4Def();
			
			password = password.substring(0, 3);
		}
	}
	
	private void setIcon1Push()
	{
		icon1TextView.setBackgroundResource(R.drawable.ic_password_push);
	}
	
	private void setIcon2Push()
	{
		icon2TextView.setBackgroundResource(R.drawable.ic_password_push);
	}
	
	private void setIcon3Push()
	{
		icon3TextView.setBackgroundResource(R.drawable.ic_password_push);
	}
	
	private void setIcon4Push()
	{
		icon4TextView.setBackgroundResource(R.drawable.ic_password_push);
	}
	
	private void setIcon1Def()
	{
		icon1TextView.setBackgroundResource(R.drawable.ic_password_def);
	}
	
	private void setIcon2Def()
	{
		icon2TextView.setBackgroundResource(R.drawable.ic_password_def);
	}
	
	private void setIcon3Def()
	{
		icon3TextView.setBackgroundResource(R.drawable.ic_password_def);
	}
	
	private void setIcon4Def()
	{
		icon4TextView.setBackgroundResource(R.drawable.ic_password_def);
	}
	
	private void setCheckPassword()
	{
		if (tempPassword.length() > 3 && password.length() > 3)
		{
			if (tempPassword.equals(password))
			{
				dialog = new MilkDialog(this, "패스워드 설정에 성공하였습니다.", new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						dialog.dismiss();
						Intent intent = new Intent();
						intent.putExtra(PASSWORD_DATA, password);
						setResult(Activity.RESULT_OK, intent);
						finish();
					}
				}, null);
				
				dialog.setLeftTitle("확인");
				dialog.show();
			}
			else
			{
				dialog = new MilkDialog(this, "이전에 입력한 패스워드와 다릅니다. 다시 입력해 주세요.", new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						password = "";
						setIcon1Def();
						setIcon2Def();
						setIcon3Def();
						setIcon4Def();
						
						dialog.dismiss();
					}
				}, null);
				
				dialog.setLeftTitle("확인");
				dialog.show();
			}
		}
		else if (password.length() > 3)
		{
			tempPassword = password;
			password = "";
			setIcon1Def();
			setIcon2Def();
			setIcon3Def();
			setIcon4Def();
			
			messageTextView.setBackgroundResource(R.drawable.text_password_re);
		}
	}
}
